package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        log.debug("MenuController__findByPage:{" + queryPageBean + "}");
        PageResult pageResult = menuService.findByPage(queryPageBean);
        log.debug(pageResult.toString());
        log.debug("获取菜单列表成功");
        return new Result(true, "获取菜单列表成功", pageResult);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Menu menu) {
        log.debug("MenuController__add:{" + menu.toString() + "}");
        menuService.add(menu);
        log.debug("新增菜单成功");
        return new Result(true, "新增菜单成功");
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id) {
        log.debug("MenuController__deleteById:{" + id + "}");
        menuService.deleteById(id);
        log.debug("菜单删除成功");
        return new Result(true, "菜单删除成功");
    }

    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.debug("MenuController__findById:{" + id + "}");
        Menu menu = menuService.findById(id);
        log.debug("获取菜单信息成功");
        return new Result(true, "获取菜单信息成功", menu);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody Menu menu) {
        log.debug("MenuController__update:{" + menu.toString() + "}");
        menuService.update(menu);
        log.debug("菜单修改成功");
        return new Result(true, "菜单修改成功");
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("MenuController:findAll: " );
        List<Menu> menuList = menuService.findAll();
        log.debug("菜单查询成功！！！");
        return new Result(true,"菜单查询成功！！！",menuList);
    }
}
